
import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_animation/utils/constant.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';

class ListPage extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _ListPageState();
  }
}

class _ListPageState extends State<ListPage>{
  late EasyRefreshController _controller;
  late ScrollController _scrollController;
  int _count = 20;
  // 是否开启加载
  final bool _enableLoad = true;

  // 控制结束
  final bool _enableControlFinish = false;

  // 是否开启刷新
  final bool _enableRefresh = true;
  

  @override
  void initState() {
    super.initState();
    _controller = EasyRefreshController();
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
    _scrollController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    _scrollController = PrimaryScrollController.of(context)!;
    return Scaffold(
      appBar: AppBar(
        title: Text(
          Constant.listTitle,
          key: Key(Constant.listTitleKey),
        ),
        leading: GestureDetector(
          key: Key(Constant.listBackKey),
          onTap: (){
            Navigator.of(context).pop();
          },
          child: Icon(Icons.arrow_back_ios),
        ),
      ),
      body:  ScrollConfiguration(
        behavior: CustomBehavior(),
        child: EasyRefresh.custom(
          key: Key(Constant.listKey),
          enableControlFinishRefresh: true,
          enableControlFinishLoad: true,
          controller: _controller,
          scrollController: _scrollController,
          header: ClassicalHeader(
            key: Key(Constant.listHeaderKey),
            refreshedText: '刷新完成',
            refreshReadyText: '松开刷新',
            refreshFailedText: '刷新失败',
            refreshingText: '刷新中...',
            noMoreText: '没有更多',
            infoText: null,
            showInfo:false,
            refreshText: '下拉刷新',
            infoColor: Colors.black,
          ),
          footer: ClassicalFooter(
            key: Key(Constant.listFooterKey),
            loadingText: '加载中...',
            loadReadyText: '上拉加载',
            loadedText: '加载完成',
            infoText: null,
            showInfo: false,
            noMoreText: '没有更多',
            infoColor: Colors.black,
          ),
          onRefresh: _enableRefresh
              ? () async {
            await Future.delayed(Duration(seconds: 2), () {
              if (mounted) {
                setState(() {
                  _count = 20;
                });
                if (!_enableControlFinish) {
                  _controller.resetLoadState();
                  _controller.finishRefresh();
                }
              }
            });
          }
              : null,
          onLoad: _enableLoad
              ? () async {
            await Future.delayed(Duration(seconds: 2), () {
              if (mounted) {
                setState(() {
                  _count += 20;
                });
                if (!_enableControlFinish) {
                  _controller.finishLoad(noMore: _count >= 80);
                }
              }
            });
          }
              : null,
          slivers: <Widget>[
            SliverList(
              delegate: SliverChildBuilderDelegate(
                    (context, index) {
                  if (index == 0) return SizedBox(height: 15.0);
                  return AvatarWrapBox(
                    key: Key('item_$index'),

                  );
                },
                childCount: _count + 1,
              ),
            ),
          ],
        ),
      ),
    );

  }
}

class AvatarWrapBox extends StatelessWidget {
  final GestureTapCallback? onTap;
  const AvatarWrapBox({Key? key, this.onTap}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () => onTap?.call(),
      child: Container(
        height: 60.0,
        padding: EdgeInsets.only(left: 15),
        margin: EdgeInsets.only(bottom: 10.0),
        child: Row(
          children: <Widget>[
            Container(
              width: 60.0,
              height: 60.0,
              margin: EdgeInsets.only(right: 12.0),
              decoration: BoxDecoration(
                color: Colors.red,
                borderRadius: BorderRadius.circular(10.0),
              ),
            ),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: <Widget>[
                Container(
                  width: 160.0,
                  height: 18.0,
                  color: Colors.red,
                ),
                Container(
                  width: 260.0,
                  height: 18.0,
                  color: Colors.red,
                ),
              ],
            ),
            Expanded(
              child: Container(
                alignment: Alignment.center,
                child: Text(key.toString().replaceAll('[<\'', '').replaceAll('\'>]', '')),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class CustomBehavior extends ScrollBehavior {
  @override
  Widget buildViewportChrome(
      BuildContext context, Widget child, AxisDirection axisDirection) {
    return child;
  }
}